package com.cloudservice.ccs.controller;


import com.cloudservice.ccs.biz.IVisitorInfoBiz;
import com.cloudservice.ccs.model.ApiResult;
import com.cloudservice.ccs.model.PageData;
import com.cloudservice.ccs.entity.VisitorInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/visitor-info")
public class VisitorInfoController {
	@Autowired
	private IVisitorInfoBiz visitorInfoBiz;

	@PostMapping("/")
	public ApiResult save(@RequestBody VisitorInfo visitorInfo) {
		return ApiResult.successWithData(visitorInfoBiz.save(visitorInfo));
	}

	@PutMapping("/{ip}")
	public ApiResult update(@PathVariable String ip, @RequestBody VisitorInfo visitorInfo) {
		visitorInfoBiz.update(ip,visitorInfo);
		return ApiResult.success();
	}

	@DeleteMapping("/{ip}")
	public ApiResult delete(@PathVariable String ip) {
		visitorInfoBiz.delete(ip);
		return ApiResult.success();
	}

	@PutMapping("/{ip}/partial")
	public ApiResult partialUpdate(@PathVariable String ip, @RequestBody Map<String, Object> fields) {
		visitorInfoBiz.partialUpdate(ip,fields);
		return ApiResult.success();
	}

	@GetMapping("/{ip}")
	public ApiResult queryById(@PathVariable String ip) {
		VisitorInfo result = visitorInfoBiz.getOne(ip);
		return ApiResult.successWithData(result);
	}

	@RequiresPermissions("all")
	@GetMapping("/")
	public ApiResult queryAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit) {
		PageHelper.startPage(page, limit);
		List<VisitorInfo> result = visitorInfoBiz.getAll();
		return ApiResult.successWithData(PageData.fromPage((Page<VisitorInfo>) result));
	}

	@PostMapping("/condition")
	public ApiResult conditionQuery(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "0") Integer limit,@RequestBody Map<String, Object> fields) {
		PageHelper.startPage(page, limit);
		List<VisitorInfo> result = visitorInfoBiz.conditionQuery(fields);
		return ApiResult.successWithData(PageData.fromPage((Page<VisitorInfo>) result));
	}
}
